package com.cloud.monitor.sys;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import io.fabric8.kubernetes.client.ConfigBuilder;
import io.fabric8.kubernetes.client.DefaultKubernetesClient;
import io.fabric8.kubernetes.client.KubernetesClient;

import io.kubernetes.client.openapi.ApiClient;
import io.kubernetes.client.openapi.ApiException;
import io.kubernetes.client.openapi.Configuration;
import io.kubernetes.client.openapi.apis.CoreV1Api;
import io.kubernetes.client.openapi.models.V1NamespaceList;
import io.kubernetes.client.openapi.models.V1Pod;
import io.kubernetes.client.openapi.models.V1PodList;
import io.kubernetes.client.util.ClientBuilder;
import io.kubernetes.client.util.Config;
import io.kubernetes.client.util.KubeConfig;
import io.kubernetes.client.util.credentials.AccessTokenAuthentication;
import org.apache.commons.lang.StringEscapeUtils;
import springfox.documentation.spring.web.json.Json;

import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;

/**
 * @author wurj
 * @time 2020/3/12 21:04
 * @description
 */
public class Test {
    public static JSONObject interfaceUtil(String path, String data) {
        String res="";
        try {
            URL url = new URL(path);
            //打开和url之间的连接
            HttpURLConnection conn = (HttpURLConnection) url.openConnection();
            PrintWriter out = null;
            //请求方式
//          conn.setRequestMethod("POST");
//           //设置通用的请求属性
            conn.setRequestProperty("accept", "*/*");
            conn.setRequestProperty("connection", "Keep-Alive");
            conn.setRequestProperty("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1)");
            //设置是否向httpUrlConnection输出，设置是否从httpUrlConnection读入，此外发送post请求必须设置这两个
            //最常用的Http请求无非是get和post，get请求可以获取静态页面，也可以把参数放在URL字串后面，传递给servlet，
            //post与get的 不同之处在于post的参数不是放在URL字串里面，而是放在http请求的正文内。
            conn.setDoOutput(true);
            conn.setDoInput(true);
            //获取URLConnection对象对应的输出流
            out = new PrintWriter(conn.getOutputStream());
            //发送请求参数即数据
            out.print(data);
            //缓冲数据
            out.flush();
            //获取URLConnection对象对应的输入流
            InputStream is = conn.getInputStream();
            //构造一个字符流缓存
            BufferedReader br = new BufferedReader(new InputStreamReader(is));
            String str = "";

            while ((str = br.readLine()) != null) {
                res+=str;
                //System.out.println(str+"****");
            }
           // System.out.println(res);

            //关闭流
            is.close();
            //断开连接，最好写上，disconnect是在底层tcp socket链接空闲时才切断。如果正在被其他线程使用就不切断。
            //固定多线程的话，如果不disconnect，链接会增多，直到收发不出信息。写上disconnect后正常一些。
            conn.disconnect();
           // System.out.println("完整结束");

        } catch (Exception e) {
            e.printStackTrace();
        }
        return JSONObject.parseObject(res);
    }

    public static void main(String[] args) throws IOException,  ApiException {
        String base="http://101.200.220.195:30836//api/v1/query?query=";
        String cpuquery="(1-((sum(increase(node_cpu{mode=\"idle\"}[1m]))by(instance))/(sum(increase(node_cpu[1m]))by(instance))))*100";
        String diskquery="(1-(((sum(node_filesystem_free{fstype=\"ext4\",mountpoint=\"/etc/hosts\"})by(instance))/1024/1024/1024)/40))*100";
        String memoryquery="(1-(sum(node_memory_MemAvailable)by(instance)/sum(node_memory_MemTotal)by(instance)))*100";
        String iopsquery="sum(node_disk_io_now)by(instance)";
        String networkreceiveQuery="(sum(irate(node_network_receive_bytes[1m]))by(instance))/1024";
        String networktransmitQuery="(sum(irate(node_network_transmit_bytes[1m]))by(instance))/1024";

//        System.out.println(query);
//        String s2 = StringEscapeUtils.unescapeJava(query);
//        System.out.println(s2);
        String url=base+iopsquery;
        System.out.println(url);
        JSONObject jsonObject=interfaceUtil(url,"");
        JSONObject jsondata= jsonObject.getJSONObject("data");
        JSONArray jsonresult= jsondata.getJSONArray("result");
        for (int i=0;i<jsonresult.size();i++){
            JSONObject jsoninstance=jsonresult.getJSONObject(i).getJSONObject("metric");
            JSONArray jsonValue=jsonresult.getJSONObject(i).getJSONArray("value");
            String ip=jsoninstance.get("instance").toString();
            String value=jsonValue.get(1).toString().substring(0);
            System.out.println(ip);
            System.out.println(value);
        }

        System.out.println("**********************************************");



    }
}
